package com.demo.client;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;

import android.content.Intent;
import android.util.Log;

public class NotificationPacketListener implements PacketListener {

	private static final String TAG = LogUtil.getClassName(NotificationPacketListener.class);
	
	private XmppManager xmppManager = null;
	
	public NotificationPacketListener(XmppManager xmpp)
	{
		this.xmppManager = xmpp;
		
	}
	@Override
	public void processPacket(Packet packet) {
	
		Log.d(TAG,"NotificationPacketListener processPacket...");
		Log.d(TAG,"packet.toXML"+packet.toXML());
		
		if(packet instanceof NotificationIQ)
		{
			NotificationIQ notificationIQ = (NotificationIQ)packet;
			
			if(notificationIQ.getChildElementXML()
					.contains("androidpn:iq:notification"))
			{
				String id = notificationIQ.getId();
				String apiKey =notificationIQ.getApiKey();
				String title = notificationIQ.getTitle();
				String message = notificationIQ.getMessage();
				String uri = notificationIQ.getUri();
				
				Intent intent = new Intent(Constants.ACTION_SHOW_NOTIFICATION);
				intent.putExtra(Constants.NOTIFICATION_ID, id);
				intent.putExtra(Constants.NOTIFICATION_API_KEY,apiKey);
				intent.putExtra(Constants.NOTIFICATION_TITLE,title);
				intent.putExtra(Constants.NOTIFICATION_MESSAGE,message);
				intent.putExtra(Constants.NOTIFICATION_URI,uri);
				
				xmppManager.getContext().sendBroadcast(intent);
			}
		}
	}
	
}
